package com.zhong.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.zhong.subject.common.enums.CategoryTypeEnum;
import com.zhong.subject.common.enums.IsDeleteFlagEnum;
import com.zhong.subject.domain.bo.SubjectLabelBO;
import com.zhong.subject.domain.convert.SubjectLabelConverter;
import com.zhong.subject.domain.service.SubjectLabelDomainService;
import com.zhong.subject.infra.basic.entity.SubjectLabel;
import com.zhong.subject.infra.basic.entity.SubjectMapping;
import com.zhong.subject.infra.basic.service.SubjectCategoryService;
import com.zhong.subject.infra.basic.service.SubjectLabelService;
import com.zhong.subject.infra.basic.service.SubjectMappingService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    SubjectLabelService subjectLabelService;

    @Resource
    SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.add.subjectLabelBO：{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeleteFlagEnum.UN_DELETE.getCode());
        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.delete.subjectLabelBO：{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeleteFlagEnum.DELETE.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.update.subjectLabelBO：{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId.subjectLabelBO：{}", JSON.toJSONString(subjectLabelBO));
        }
        //如果当前分类时1级分类，则查询所有标签
        Integer categoryType = subjectCategoryService.queryById(subjectLabelBO.getCategoryId()).getCategoryType();
        if (CategoryTypeEnum.PRIMARY.getCode() == categoryType) {
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> subjectLabelList = subjectLabelService.queryByCondition(subjectLabel);
            return SubjectLabelConverter.INSTANCE.convertLabelListToBoList(subjectLabelList);
        }
        //否则查询
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDelete(IsDeleteFlagEnum.UN_DELETE.getCode());
        //查询map表中的映射关系
        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelId(subjectMapping);
        if(CollectionUtils.isEmpty(subjectMappingList)){
            return Collections.emptyList();
        }
        if(log.isInfoEnabled()){
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId.subjectMappingList：{}", JSON.toJSONString(subjectMappingList));
        }
        //抽取所有labelId
        List<Long> labelIdList = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> subjectLabelList = subjectLabelService.batchQueryById(labelIdList);
        //转换成BO数据
        List<SubjectLabelBO> subjectLabelBOList = new LinkedList<>();
        subjectLabelList.forEach(subjectLabel -> {
            SubjectLabelBO labelBO = new SubjectLabelBO();
            labelBO.setId(subjectLabel.getId());
            labelBO.setLabelName(subjectLabel.getLabelName());
            labelBO.setCategoryId(categoryId);
            subjectLabelBOList.add(labelBO);
        });
        return subjectLabelBOList;
    }
}
